package com.x.erp.manage;

import com.x.erp.model.Car;
import com.x.erp.model.CarLog;
import com.x.erp.model.CarSale;
import com.x.erp.util.ConstantState;
import com.x.erp.util.ConstantTopic;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.manage.BaseManage;
import com.x.rocketmq.RocketMqProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 销售出库信息Mapper
 */
@Service
public class CarSaleManage extends BaseManage {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RocketMqProducer rocketMqProducer;

	/**
	 * 新增销售出库信息
	 * @param carSale
	 * @return
	 * @throws Exception
	 */
	public CarSale insertCarSale(CarSale carSale, CarLog carLog) throws Exception {
		Date cDate = this.getDate(0);
		carSale.setSaleCreateTime(cDate);
		carSale.setSaleUpdateTime(cDate);
		//新增销售出库信息
		carSale = this.insert(carSale);
		Car car = new Car();
		car.setCarId(carSale.getCarId());
		car.setCarStockState(ConstantState.CAR_STOCK_STATE_OUT_FINISH);//出库
		car.setCarSaleState(ConstantState.CAR_SALE_STATE_OFFSALE);//下架
		car.setCarStockTime(cDate);
		car.setCarUpdateTime(cDate);
		this.update(car);//修改车辆库存状态
		//记录日志信息
		this.insert(carLog);
		try {
			rocketMqProducer.produceMessage(ConstantTopic.TOPIC_ERP_CAR, ConstantTopic.TAG_CAR_STOCK_STATE_40, car);
		} catch (Exception e) {
			logger.error("设置销售出库信息插入rocketMq队列异常", e);
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0003, e);
		}
		return carSale;
	}

}
